package com.android.lgc.android_mvp.base;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.widget.Toast;

import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.OnShowRationale;
import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.RuntimePermissions;


/**
 * 抽象:抽象出解绑与绑定操作
 * 注意:为了能够兼容多个模块,兼容多个Activity,所以采用泛型设计
 */
@RuntimePermissions
public abstract class BaseActivity<V extends BaseView,P extends BasePresenter<V>> extends AppCompatActivity {

    private P presenter;
    private V view;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //动态申请权限
        startWriteStorageWithCheck();

        if(presenter==null){
            presenter=createPresenter();
        }

        if(view ==null){
            view=createView();
        }

        if(presenter!=null && view!=null){
            presenter.attachView(view);
        }
    }

    public abstract P createPresenter();
    public abstract V createView();

    public P getPresenter(){
        return presenter;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(presenter!=null && view!=null){
            presenter.detachView();
        }
    }

    //写入权限
    //不是直接调用的方法
    @NeedsPermission({Manifest.permission.WRITE_EXTERNAL_STORAGE})
    public void startWriteStorage()
    {
        // 授权后的操作(回调)
    }

    //这个是真正调用的方法
    public void startWriteStorageWithCheck()
    {
//        BaseActivityPermissionsDispatcher.startWriteStorageWithCheck(this);
    }

    @OnPermissionDenied({Manifest.permission.WRITE_EXTERNAL_STORAGE})
    void onWriteStorageDenied()
    {
        Toast.makeText(this, "请打开写入权限", Toast.LENGTH_LONG).show();
    }

    @OnNeverAskAgain({Manifest.permission.WRITE_EXTERNAL_STORAGE})
    void onWriteStorageNever() {
        Toast.makeText(this, "写入权限被永久禁止", Toast.LENGTH_LONG).show();
    }

    @OnShowRationale({Manifest.permission.WRITE_EXTERNAL_STORAGE})
    void onWriteStorageRationale(PermissionRequest request) {
        showRationaleDialog(request, "本软件需要使用写入权限来保证功能的正常使用，请点击同意接受权限");
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//        BaseActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }

    //权限被拒绝后，再次使用此权限时会显示对话框，里面可以显示一些理由来引导用户同意权限
    private void showRationaleDialog(final PermissionRequest request, final String message) {
        new AlertDialog.Builder(this)
                .setPositiveButton("同意", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        request.proceed();
                    }
                })
                .setNegativeButton("禁止", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        request.cancel();
                    }
                })
                .setCancelable(false)
                .setMessage(message)
                .show();
    }
}